import os

import pandas as pd
import pyomo.environ as pe


def market_outcome(m: pe.ConcreteModel(), results_dir: str):
    # create this data frame to store the production of Generators and market prices
    results_df = pd.DataFrame(0.0, columns=[f"G{g.id}" for g in m.generators] + ["Price"],
                              index=m.time_steps)
    # insert the data
    for t in m.time_steps:
        results_df.at[t, "Price"] = round(m.dual[m.Market_Balance[t]], ndigits=2)
        for g in m.generators:
            results_df.at[t, f"G{g.id}"] = round(m.production[g, t].value, ndigits=2)

    # output the csv file
    results_df.to_csv(os.path.join(results_dir, "results.csv"), sep=',')

    print("Output is in " + results_dir)
